package com.zz.schoolwall.controller;

import com.zz.common.utils.ShiroUtils;
import com.zz.common.utils.StringUtils;
import com.zz.schoolwall.domain.SchoolWallLikeDO;
import com.zz.schoolwall.service.SchoolWallCommentService;
import com.zz.schoolwall.service.SchoolWallLikeService;
import com.zz.schoolwall.service.SchoolWallService;
import com.zz.util.ResultVOUtil;
import com.zz.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api/schoolwall/like")
@Controller
public class ApiSchoolWallLikeController {
	@Autowired
	SchoolWallLikeService schoolWallLikeService;
	@Autowired
	SchoolWallCommentService schoolWallCommentService;
	@Autowired
	SchoolWallService schoolWallService;

	private final String liketype_wall="wall";
	private final String liketype_comment="comment";
	/**
	 * 墙话点赞
	 */
	@PostMapping("/saveWallLike")
	@ResponseBody
	public ResultVO saveWallLike(@RequestParam Long typeId ) {
		SchoolWallLikeDO schoolWallLikeDO =new SchoolWallLikeDO();
		schoolWallLikeDO.setLikeType(liketype_wall);
		schoolWallLikeDO.setLikeTypeId(typeId);
		schoolWallLikeDO.setLikeCreateman(ShiroUtils.getUserId()+"");
		int count=schoolWallLikeService.save(schoolWallLikeDO);
		schoolWallService.increaseLikeNum(typeId);
		if (count>0) {
			return ResultVOUtil.success( schoolWallLikeDO.getLikeId());
		}
		return ResultVOUtil.error(-1,"失败");
	}
	/**
	 * 评论点赞
	 */
	@PostMapping("/saveCommentLike")
	@ResponseBody
	public ResultVO saveCommentLike(@RequestParam Long typeId ) {
		SchoolWallLikeDO schoolWallLikeDO =new SchoolWallLikeDO();
		schoolWallLikeDO.setLikeType(liketype_comment);
		schoolWallLikeDO.setLikeTypeId(typeId);
		schoolWallLikeDO.setLikeCreateman(ShiroUtils.getUserId()+"");
		int count=schoolWallLikeService.save(schoolWallLikeDO);
		schoolWallCommentService.increaseLikeNum(typeId);
		if (count>0) {
			return ResultVOUtil.success( schoolWallLikeDO.getLikeId());
		}
		return ResultVOUtil.error(-1,"失败");
	}

	@PostMapping("/cancelLike")
	@ResponseBody
	public ResultVO cancelWallLike(@RequestParam Long likeId ) {
		SchoolWallLikeDO schoolWallLikeDO=schoolWallLikeService.get(likeId);
		if(StringUtils.isNotBlank(schoolWallLikeDO.getLikeType())){
			return ResultVOUtil.error(-1,"失败:");
		}
		int count=schoolWallLikeService.remove(likeId);
		if(liketype_wall.equals(schoolWallLikeDO.getLikeType())){
			schoolWallService.decreaseLikeNum(schoolWallLikeDO.getLikeTypeId());
		}
		if(liketype_comment.equals(schoolWallLikeDO.getLikeType())){
			schoolWallCommentService.decreaseLikeNum(schoolWallLikeDO.getLikeTypeId());
		}
		if (count>0) {
			return ResultVOUtil.success();
		}
		return ResultVOUtil.error(-1,"失败");
	}
}
